﻿using System;
using MarriageFertility.Texts;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.Overlay;

namespace MarriageFertility.Helpers;

public class GameMenuHelper
	{
		public GameMenuHelper(CampaignGameStarter gameStarter)
		{
			campaignGameStarter = gameStarter;
		}
		
		public void Add(string menuId, string menuText, OnInitDelegate initMenuAction = null)
		{
			campaignGameStarter.AddGameMenu(menuId, menuText, initMenuAction, GameOverlays.MenuOverlayType.SettlementWithBoth, GameMenu.MenuFlags.None, null);
		}
		
		public void AddBasicOption(string menuId, string optionId, string optionText, GameMenuOption.LeaveType leaveType, GameMenuOption.OnConsequenceDelegate completeAction = null, int index = -1)
		{
			campaignGameStarter.AddGameMenuOption(menuId, menuId + "_" + optionId, optionText, delegate(MenuCallbackArgs args)
			{
				args.optionLeaveType = leaveType;
				return true;
			}, completeAction, false, index, false);
		}
		
		public void AddContinueOption(string menuId, string optionId, string optionText, string targetMenuId, GameMenuOption.LeaveType leaveType = GameMenuOption.LeaveType.Continue, int index = -1)
		{
			AddBasicOption(menuId, optionId, optionText, leaveType, delegate(MenuCallbackArgs args)
			{
				GameMenu.SwitchToMenu(targetMenuId);
			}, index);
		}
		
		public void AddResumeOption(string menuId, string resumeMenuId)
		{
			campaignGameStarter.AddGameMenuOption(menuId, "resume", TournamentText.ResumeText.ToString(), delegate(MenuCallbackArgs args)
			{
				args.optionLeaveType = GameMenuOption.LeaveType.Leave;
				return true;
			}, delegate(MenuCallbackArgs args)
			{
				GameMenu.SwitchToMenu(resumeMenuId);
			});
		}
		
		public void AddResumeOption(string menuId, Action returnAction)
		{
			campaignGameStarter.AddGameMenuOption(menuId, "resume", "{=adMenuResume}Resume", delegate(MenuCallbackArgs args)
			{
				args.optionLeaveType = GameMenuOption.LeaveType.Leave;
				return true;
			}, delegate(MenuCallbackArgs args)
			{
				returnAction();
			});
		}
		
		public void AddConditionOption(string menuId, string optionId, string optionText, GameMenuOption.LeaveType leaveType, Func<MenuCallbackArgs, bool> conditionAction, GameMenuOption.OnConsequenceDelegate completeAction = null, int index = -1)
		{
			campaignGameStarter.AddGameMenuOption(menuId, menuId + "_" + optionId, optionText, delegate(MenuCallbackArgs args)
			{
				args.optionLeaveType = leaveType;
				return conditionAction(args);
			}, completeAction, false, index);
		}
		
		public CampaignGameStarter campaignGameStarter;
	}